package com.zzyl.nursing.service.impl.member;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocalUtil;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.member.MemberReservationMapper;
import com.zzyl.nursing.service.member.MemberReservationService;
import com.zzyl.nursing.vo.memberVo.TimeCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {

    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Override
    public int getUserCancelledCount(Long id) {

        // 计算当天时间 00:00:00 和 下一天过一秒的两个值
        long time = System.currentTimeMillis();
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        // 获取今天00:00:00
        LocalDateTime begin = localDateTime.toLocalDate().atStartOfDay();
        // 获取明天00:00:00
        LocalDateTime end = begin.plusDays(1);
        String username = UserThreadLocalUtil.getUserName();
        int userCancelledCount = memberReservationMapper.getUserCancelledCount(id, begin, end);
        if(userCancelledCount == 2) return 3;
        log.info("用户{}取消的预约数量为{}", username , userCancelledCount);
        return userCancelledCount;

    }

    /**
     * 查询指定时间段预约数量
     * @return
     */
    @Override
    public List<TimeCountVo> getCountByTime() {

        long time = System.currentTimeMillis();
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        // 计算当天开始时间00:00:00
        LocalDateTime begin = localDateTime.toLocalDate().atStartOfDay();
        // 计算下一天开始时间00:00:00
        LocalDateTime end = begin.plusDays(1);

        return memberReservationMapper.getCountByTime(begin, end);

    }

    @Override
    public List<Reservation> getPageList() {

//        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Reservation::getStatus, status);
        return list();
    }

    @Override
    public List<Reservation> selectReservationStatus() {

        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        // 条件是 状态为0、当前时间大于预约时间
        queryWrapper.le(Reservation::getTime, LocalDateTime.now())
                    .eq(Reservation::getStatus, 0);

        return list(queryWrapper);
    }

    @Override
    public void updateReservationById(Long id) {

        Reservation one = getOne(new LambdaQueryWrapper<Reservation>().eq(Reservation::getId, id));
        one.setStatus(2);
        updateById(one);
    }
}
